package samples.csv;

import java.util.List;

import com.google.code.jholidays.core.DescriptorCollection;
import com.google.code.jholidays.core.EventBuilder;
import com.google.code.jholidays.events.IEvent;
import com.google.code.jholidays.exceptions.NotSupportedEventException;
import com.google.code.jholidays.io.IDescriptorReader;
import com.google.code.jholidays.io.csv.CsvReader;

/**
 * This example loads event descriptors from CSV file, builds events and prints
 * their information and occurrence date (for 2010 year) onto the terminal.
 * 
 * @author tillias
 * 
 */
public class CsvReadExample {

    public static void main(String[] args) throws IllegalArgumentException,
	    NotSupportedEventException {

	IDescriptorReader reader = new CsvReader("samples\\csv\\input.csv", ";");
	DescriptorCollection collection = reader.read();

	if (collection == null) {
	    System.out
		    .println("Invalid input CSV file. Can't read descriptors. Exiting");
	    System.exit(-1);
	} else {
	    System.out
		    .println("Event descriptors has been loaded. Building events...\n");

	    EventBuilder builder = new EventBuilder();

	    int calcYear = 2010;
	    List<IEvent> events = builder.buildEvents(collection);

	    for (IEvent event : events) {
		System.out.println("Event name: " + event.getName());
		System.out.println("Event description: "
			+ event.getDescription());
		System.out.println("Event date in " + calcYear + ": "
			+ event.getDate(2010));
		System.out.println();
	    }
	}
    }
}
